package com.woniuxy.background.dao;

import com.woniuxy.background.pojo.Menu;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.stereotype.Component;

import java.util.List;

@Mapper
@Component
public interface MenuMapper {
    int deleteByPrimaryKey(Integer menuId);

    int insert(Menu record);

    int insertSelective(Menu record);

    Menu selectByPrimaryKey(Integer menuId);

    int updateByPrimaryKeySelective(Menu record);

    int updateByPrimaryKey(Menu record);

    @Select("select m.* from menu m left join role_menu rm on rm.menu_id = m.menu_id " +
            " left join role r on r.role_id = rm.role_id where r.role_id = #{value}")
    List<Menu> findByRoleId(int roleId);

    @Select("select * from menu order by sorting desc")
    List<Menu> selectAll();

    @Select("select perms from menu m left join role_menu rm on rm.menu_id = m.menu_id " +
            "left join role r on r.role_id = rm.role_id where r.role_id = #{value}")
    List<String> selectPermsByRoleId(int roleId);

    @Select("select * from menu where title = #{value}")
    List<Menu> findByTitle(String title);

    @Update("update menu set sorting = #{sorting} where menu_id = #{menuId}")
    void updateSorting(@Param("menuId") int menuId, @Param("sorting") int sorting);

    @Select("select * from menu where parent_id = #{value}")
    List<Menu> selectByParentId(int id);
}